<?php

namespace App\Service\YuanYu;

use App\Constants\ErrorCode;
use App\Helpers\Helper;
use App\Job\HostGreetJob;
use App\Model\HostPushLog;
use App\Model\User;
use App\Model\UserAutoMsg;
use App\Model\UserProfile;
use App\Model\Yuanyu\UserHitStart;
use App\Service\User\UserBaseService;

/**
 * 开始搭讪
 */
class StartHittingService extends UserBaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $option = $this->request->input('op', 1);
        if ($option == 0) {
            UserHitStart::stopHit($userId);
            return ['result' => 1];
        }

        //是否主播
        $userProfile = UserProfile::getProfile($userId);
//        $checkResult = $this->checkHostVrfy($userId, $userProfile);
        $checkResult = $this->checkHostVrfyV2($userId, $userProfile->created_at, $userProfile);
        if ($checkResult['errcode'] > 0) {
            return $this->setError($checkResult['errcode'], $checkResult['errmsg']);
        }

        $autoMsgs = UserAutoMsg::pickMultiUserMsgs($userId);
        if (empty($autoMsgs)) {
            return $this->setError(ErrorCode::NO_AUTO_MSG, '没有设置自动回复消息');
        }

        //先停止之前记录
        UserHitStart::stopHit($userId);
        //搭讪记录
        UserHitStart::addRecord($userId, 6, 2);
        //搭讪池
        $pushTimes = explode(',', '10-1,30-2,50-2');
        foreach ($pushTimes as $batch => $pushSet) {
            $setting = explode('-', $pushSet);
            $delayTime = intval($setting[0]);
            $pushNum = intval($setting[1]);
            Helper::pushRedisAsync(new HostGreetJob([
                'user_id' => $userId,
                'push_num' => $pushNum,
                'batch_no' => $batch + 1,
                'greet_type' => HostPushLog::TYPE_YUANYU,
                'host_to_user' => 1
            ]), $delayTime, 'push_msg');
        }
        return [
            'hit_speed' => 2,
            'hit_tip' => '推荐分达到90分后，搭讪速度可提升至5倍'
        ];
    }
}
